package server;

import java.io.*;
import java.util.zip.*;

public class SerializeHelper 
{
	public static byte[] PackagetoByte(ResourcePackage resourcePackage) throws IOException
	{
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		ZipOutputStream zos = new ZipOutputStream(bos);
		ZipEntry entry = new ZipEntry("");
		zos.putNextEntry(entry);
    	ObjectOutputStream oos = new ObjectOutputStream(zos);
    	oos.writeObject(resourcePackage);
    	oos.close();
        return bos.toByteArray();
    }
    public static ResourcePackage BytetoPackage (byte[] bytes) throws IOException, ClassNotFoundException
    {
    	ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
    	ZipInputStream zis = new ZipInputStream(bis);
    	ZipEntry entry = zis.getNextEntry();
    	ObjectInputStream ois = new ObjectInputStream(zis);
    	ResourcePackage rp = (ResourcePackage)ois.readObject();
    	return rp;
    }
    public static void PackagetoFile(ResourcePackage resourcePackage, String filePath) throws IOException
    {
    	FileOutputStream fos = new FileOutputStream(filePath);
    	ObjectOutputStream oos = new ObjectOutputStream(fos);
    	oos.writeObject(resourcePackage);
    	oos.close();
    }
    public static ResourcePackage FiletoPackage(String filePath) throws IOException, ClassNotFoundException
    {
    	FileInputStream fis = new FileInputStream(filePath);
    	ObjectInputStream ois = new ObjectInputStream(fis);
    	ResourcePackage rp = (ResourcePackage) ois.readObject();
    	ois.close();
    	return rp;
    }
}
